package com.xinjian.jianba.service.impl;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xinjian.jianba.dao.dwmid.SiteListDao;
import com.xinjian.jianba.dao.dwmid.UserLocationDao;
import com.xinjian.jianba.dao.dwmid.UserMediaDao;
import com.xinjian.jianba.dao.dwmid.UserMonmentsDao;
import com.xinjian.jianba.dao.dwmid.UserTrackDao;
import com.xinjian.jianba.dto.InviteMeetDto;
import com.xinjian.jianba.dto.MeetAddressDto;
import com.xinjian.jianba.dto.MeetAddressVo;
import com.xinjian.jianba.dto.ShopListDto;
import com.xinjian.jianba.dto.UserMonmentsDto;
import com.xinjian.jianba.dto.UserMonmentsVo;
import com.xinjian.jianba.dto.VedioDto;
import com.xinjian.jianba.enity.SiteListEntity;
import com.xinjian.jianba.enity.UserLocationEnity;
import com.xinjian.jianba.enity.UserMediaEnity;
import com.xinjian.jianba.enity.UserMonmentsEntity;
import com.xinjian.jianba.enity.UserTrackEntity;
import com.xinjian.jianba.exception.BusinessException;
import com.xinjian.jianba.exception.ResponseCode;
import com.xinjian.jianba.service.SiteListService;
import com.xinjian.jianba.utils.TimeUtil;
import com.xinjian.jianba.utils.Utils;

@Service
public class SiteListServiceImpl implements SiteListService {
	@Autowired
	private SiteListDao siteListDao;
	@Autowired
	private UserMonmentsDao userMonmentsDao;
	@Autowired
	private UserTrackDao userTrackDao;
	@Autowired
	private UserLocationDao userLocationDao;
	@Autowired
	private UserMediaDao userMediaDao;

	@Transactional(rollbackFor = Exception.class)
	public UserMonmentsVo saveSiteListAndUserMonments(UserMonmentsDto userMonmentsDto) throws FileNotFoundException, IOException, ParseException {
		String isTrack=userMonmentsDto.getIsTrack();
		String url = userMonmentsDto.getUrl();
		Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// String now=sdf.format(date);
		// String year=now.substring(0, 4);
		// String month=now.substring(5,10).replaceAll("-", "");
		String site_id = userMonmentsDto.getSiteId().trim();
		SiteListEntity siteListEntity = new SiteListEntity();
		try {
			siteListEntity.setSiteId(site_id);
			siteListEntity.setSiteName(userMonmentsDto.getAdressName());
			siteListEntity.setLatfloat(userMonmentsDto.getLatfloat());
			siteListEntity.setLngfloat(userMonmentsDto.getLngfloat());
			siteListEntity.setAddTime(timestamp);
			siteListEntity.setTimeAdd(date);
			siteListDao.insert(siteListEntity);
		} catch (DuplicateKeyException e) {
			
		}
		UserMonmentsEntity userMonmentsEntity = new UserMonmentsEntity();
		userMonmentsEntity.setAddTime(timestamp);
		userMonmentsEntity.setUserId(Long.parseLong(userMonmentsDto.getUserId()));
		userMonmentsEntity.setSiteId(site_id);
		userMonmentsEntity.setSiteName(userMonmentsDto.getAdressName());
		userMonmentsEntity.setLatfloat(userMonmentsDto.getLatfloat());
		userMonmentsEntity.setLngfloat(userMonmentsDto.getLngfloat());
		userMonmentsEntity.setDetail(userMonmentsDto.getContent());
		userMonmentsEntity.setMedia_adress(url);
		userMonmentsEntity.setSaveTime(timestamp);
		userMonmentsEntity.setTimeAdd(date);
		userMonmentsDao.insert(userMonmentsEntity);
		Long mon_id = userMonmentsEntity.getMonId();
		
		 //视频朋友圈上传需在下面的if条件中作额外处理,图片朋友圈上传跳过此处理
		 if(url.indexOf("http")<0){
			 VedioDto vedioDto=new VedioDto();
			 vedioDto.setUserId(userMonmentsDto.getUserId());
			 vedioDto.setVideoId(url);
			 vedioDto.setImgUrl(userMonmentsDto.getImgStr()==null?"":userMonmentsDto.getImgStr());
			 saveVideoUrl(vedioDto);
	     }

		if(isTrack.equals("1")){
		UserTrackEntity userTrackEntity = new UserTrackEntity();
		userTrackEntity.setAddTime(new Timestamp(Utils.StringToDate(userMonmentsDto.getPublishTime()).getTime()));
		userTrackEntity.setGeom(userMonmentsDto.getGEOM());
		userTrackEntity.setLatfloat(userMonmentsDto.getLatfloat());
		userTrackEntity.setLngfloat(userMonmentsDto.getLngfloat());
		userTrackEntity.setSiteId(site_id);
		userTrackEntity.setTimeAdd(Utils.StringToDate(userMonmentsDto.getPublishTime()));
		userTrackEntity.setUserId(Long.parseLong(userMonmentsDto.getUserId()));
		userTrackEntity.setSiteName(userMonmentsDto.getAdressName());
		userTrackDao.insert(userTrackEntity);

		UserLocationEnity userLocationEnity = new UserLocationEnity();
		userLocationEnity.setAddDate(Utils.StringToDate(userMonmentsDto.getPublishTime()));
		userLocationEnity.setAddTime(new Timestamp(Utils.StringToDate(userMonmentsDto.getPublishTime()).getTime()));
		userLocationEnity.setGeom(userMonmentsDto.getGEOM());
		userLocationEnity.setId(userMonmentsDto.getUserId());
		userLocationEnity.setLatfloat(userMonmentsDto.getLatfloat());
		userLocationEnity.setLngfloat(userMonmentsDto.getLngfloat());
		userLocationDao.insert(userLocationEnity);
		}
		UserMonmentsVo userMonmentsVo = new UserMonmentsVo();
		userMonmentsVo.setMon_id(mon_id);
		userMonmentsVo.setSave_time(TimeUtil.getTimeFormatText(Utils.convertTimestampToString(timestamp)));
		return userMonmentsVo;

	}

	@Override
	public List<SiteListEntity> findAll() {
		return siteListDao.findAll();
	}

	@Override
	public SiteListEntity findBySiteId(InviteMeetDto inviteMeetDto) {
		return siteListDao.findBySiteId(inviteMeetDto);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public int save(SiteListEntity siteListEntity) {
		return siteListDao.save(siteListEntity);
	}
	@Transactional(rollbackFor = Exception.class)
	public int saveVideoUrl(VedioDto vedioDto) {
		int result=0;
		Date date=new Date();
		UserMediaEnity userMediaEnity=new UserMediaEnity();
		userMediaEnity.setVideoId(vedioDto.getVideoId());
		userMediaEnity.setUid(Long.parseLong(vedioDto.getUserId()));
		List<UserMediaEnity> list=userMediaDao.select(userMediaEnity);
		if(list.size()>0){
			throw new BusinessException(ResponseCode.ERROR, "该视频已上传");
		}
		userMediaEnity.setUid(null);
		List<UserMediaEnity> listCheck=userMediaDao.select(userMediaEnity);
		if(listCheck.size()>0){
			userMediaEnity.setUid(Long.parseLong(vedioDto.getUserId()));
			userMediaEnity.setMtype("monsVod");
			userMediaEnity.setImgStr(vedioDto.getImgUrl());
			userMediaDao.updateVod(userMediaEnity);
		}else{
			try{
			userMediaEnity.setUid(Long.parseLong(vedioDto.getUserId()));
			userMediaEnity.setType(1);
			userMediaEnity.setVideoId(vedioDto.getVideoId());
			userMediaEnity.setAddress(vedioDto.getVideoId());
			userMediaEnity.setMtype("monsVod");
			userMediaEnity.setImgStr(vedioDto.getImgUrl());
			userMediaEnity.setCreTime(new Timestamp(date.getTime()));
			userMediaEnity.setTimeAdd(date);
			result=userMediaDao.insertVideo(userMediaEnity);
			}catch(DuplicateKeyException e){
				UserMediaEnity userMedia=new UserMediaEnity();
				userMedia.setUid(Long.parseLong(vedioDto.getUserId()));
				userMediaEnity.setMtype("monsVod");
				userMediaEnity.setImgStr(vedioDto.getImgUrl());
				result=userMediaDao.updateVod(userMediaEnity);
			}
		}
		return result;
	}


}
